<template>
  <forms ref="forms" :config="config" @submit:forms="onsubmit" :btns="btns">
    <template v-slot:api_result="{row}">
      <json_param v-if="row.value" :value="row.value" is_info="1"></json_param>
      <el-button v-else-if="in_right_admin('UserRealName/check') && id" size="small" type="primary" @click="check">身份证校验</el-button>
    </template>
  </forms>
</template>

<script>
import {_config} from "@/js/s94_tool.js";
import s94Mjs from "s94-js";
import {ajax} from "@/js/api_tool.js";
import {iframe, in_right_admin} from "@/js/tool.js";
import json_param from "@/components/auto_import/UnifyParam/json_param.vue";

export default {
  components: {json_param},
  props: ['__iframe'],
  data(){
    let id = this.$get('id'), btns = undefined;

    let status_config = _config('status_alias_map.user_real_name.status');
    let forms_config = {
      name: {label:'姓名',name:'name',type:'text_one'},
      id_card: {label:'身份证号',name:'id_card',type:'text_one'},
      id_card_front: {label:'身份证正面',name:'id_card_front',type:'file_img'},
      id_card_back: {label:'身份证背面',name:'id_card_back',type:'file_img'},
    }
    if (id){
      s94Mjs.each(forms_config, function (row){ row.is_info = true; });
      forms_config = Object.assign(forms_config, {
        api_result: {label:'核对接口返回',name:'api_result',type:'api_result',value:''},
        expiration_time: {label:'过期时间',name:'expiration_time',type: 'date',tips:'核对提交的身份证照片的有效期限来设定过期时间，为空表示永不过期'},
        status: {label:'状态',name:'status',type: 'radio',config:status_config},
        admin_note: {label:'备注',name:'admin_note',type: 'text_more'},
        create_time: {label:'申请时间',name:'create_time',tips:''},
      })
    }else{
      forms_config = Object.assign({
        user_id: {label:'指定用户',name:'user_id',type:'list_id',config:'user/index',tips:''}
      }, forms_config);
    }
    if (this.$get('is_info')){
      s94Mjs.each(forms_config, function (row){ row.is_info = true; });
      btns = [];
    }
    return {
      id: id,
      btns: btns,
      config: forms_config,
    }
  },
  methods: {
    in_right_admin: in_right_admin,
    onsubmit(formData){
      let url = this.id ? `/admin/user_real_name/edit?id=`+this.id : '/admin/user_real_name/add';
      ajax({url:url, method:'POST', data:formData}, (res)=>{
        if (this.__iframe){
          iframe.exe('user_real_name/index', '$refs.list_url.list');
          iframe.hide('user_real_name/edit');
          if (!this.id) this.$refs['forms'].init();
        }else {
          this.$router.push('index');
        }
      }, (res)=>{
        if (res.errors) this.$refs['forms'].errors = res.errors;
      })
    },
    check(){
      let app = this;
      ajax({url:'/admin/user_real_name/check?id='+this.id},function(res){
        app.$refs['forms'].init(res.data);
        return false
      })
    }
  },
  mounted() {
    let app = this;
    if (!this.__iframe) window['app'] = this;
    if (this.id) {
      ajax({url:'/admin/user_real_name/info?id='+this.id, cache:true}, (res)=>{
        this.$refs['forms'].init(res.data);
        return false;
      }, ()=>{
        if (this.__iframe){
          iframe.remove('user_real_name/edit');
        }else {
          this.$router.push('index');
        }
      })
    }

    //其他初始化操作
  }
}
</script>
